/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuMethodApp.java
 *
 * Created on 06-oct-2010, 13:27:15
 */
package interfaz;

//import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.*;
import org.pushingpixels.substance.api.SubstanceConstants;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceBusinessBlueSteelLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;
//import org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel;
//import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;

/**
 *
 * @author The Nu-Method Team
 */
public class NuMethodApp extends javax.swing.JFrame {

    DialogoAboutUs about = null;
    PanelMain panelMain;
    PanelWelcome panelWelcome;
    PanelGraficas panelGrafica;
    CardLayout cl;
    static Properties Functions;
    private SubstanceOfficeBlue2007LookAndFeel officeBlueLAF = null;
    private SubstanceOfficeSilver2007LookAndFeel officeSilverLAF = null;
    private SubstanceBusinessBlueSteelLookAndFeel blueSteelLAF = null;
    private SubstanceGraphiteGlassLookAndFeel graphiteLAF = null;
    private SubstanceAutumnLookAndFeel autumnLAF = null;
    //Strings que representan las funciones con las que los metodos numericos realizaran sus operaciones
    private static String f = new String();
    private static String df = new String();
    private static String d2f = new String();
    private static String g = new String();
    private static String h = new String();

    // <editor-fold defaultstate="collapsed" desc="getters y setters para las funciones de la aplicacion">
    public static String getF() {
        return f;
    }

    public static void setF(String f) {
        NuMethodApp.f = f;
    }

    public static String getDf() {
        return df;
    }

    public static void setDf(String df) {
        NuMethodApp.df = df;
    }

    public static String getG() {
        return g;
    }

    public static void setG(String g) {
        NuMethodApp.g = g;
    }

    public static String getH() {
        return h;
    }

    public static void setH(String h) {
        NuMethodApp.h = h;
    }

    public static String getD2f() {
        return d2f;
    }

    public static void setD2f(String d2f) {
        NuMethodApp.d2f = d2f;
    }

    public static Properties getFunctions() {
        return Functions;
    }

    public PanelMain getPanelMain() {
        return panelMain;
    }
    // </editor-fold>
    // Constantes usadas para el cambio de panel en el inicio.
    public static final int WELCOME = 0;
    public static final int MAIN = 1;
    public static final int GRAFICAS = 2;

    //
    /** Creates new form NuMethodApp */
    public NuMethodApp() {
        try {
            Functions = new Properties();
            FileInputStream in = new FileInputStream("." + File.separator + "Save.nmf");
            Functions.load(in);
            in.close();

            loadSavedFunctions();

            initComponents();
            initComponents2();

            about = new DialogoAboutUs(this, rootPaneCheckingEnabled);
            about.setVisible(false);
            about.setLocationRelativeTo(null);

            this.setTitle("Nu-Met#od");
            this.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\NuMethodIcon.png"));
            super.setLocationRelativeTo(null);
            //blueSteelLAF =  new SubstanceBusinessBlueSteelLookAndFeel();
            autumnLAF = new SubstanceAutumnLookAndFeel();
            configureAllHyperlinksColor(Color.black);

            UIManager.setLookAndFeel(autumnLAF);
            SubstanceLookAndFeel.setWidgetVisible(this.getRootPane(),
                    true,
                    SubstanceConstants.SubstanceWidgetType.TITLE_PANE_HEAP_STATUS);
            SwingXUtilities.updateAllComponentTreeUIs();

            cl = (CardLayout) this.getContentPane().getLayout();

            pack();


        } catch (FileNotFoundException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Imposible encontrar el archivo Save.nmf"
                    + "\n No se pueden encontrar las ecuaciones de la aplicacion", "Imposible hallar las ecuaciones", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public final void configureAllHyperlinksColor(Color clickedColor) {
        panelWelcome.setLinksColor(clickedColor);
        panelGrafica.setLinksColor(clickedColor);
        panelMain.setLinksColor(clickedColor);
        panelMain.panelEcuaciones.setLinksColor(clickedColor);
        panelMain.panelInterNewton.setlinksColor(clickedColor);
        panelMain.panelLagrange.setlinksColor(clickedColor);
        panelMain.panelSplinesCuadraticos.setlinksColor(clickedColor);
        panelMain.panelSplinesCubicos.setlinksColor(clickedColor);
        panelMain.panelSplinesLineales.setlinksColor(clickedColor);

    }

    /**
     * Metodo interino de la aplicacion utilizado en el inicio para
     * cargar las funciones halladas en disco
     */
    private void loadSavedFunctions() {
        String fTemp = Functions.getProperty("function-F");
        String fPrimaTemp = Functions.getProperty("function-FPrima");
        String f2PrimaTemp = Functions.getProperty("function-F2Prima");
        String gTemp = Functions.getProperty("function-G");
        String gPrimaTemp = Functions.getProperty("function-GPrima");
        String hTemp = Functions.getProperty("function-H");
        String hPrimaTemp = Functions.getProperty("function-HPrima");
        if (fTemp != null) {
            if (fPrimaTemp != null) {
                if (f2PrimaTemp != null) {
                    if (gTemp != null) {
                        if (gPrimaTemp != null) {
                            if (hTemp != null) {
                                if (hPrimaTemp != null) {
                                    //Si Todas las verificaciones son ciertas, settear los valores iniciales
                                    f = fTemp;
                                    df = fPrimaTemp;
                                    d2f = f2PrimaTemp;
                                    g = gTemp;
                                    h = hTemp;
                                }//Hallar HPrima
                            }//Hallar H
                        } //Hallar GPrima
                    }//Hallar G
                }//Hallar F2Prima
            }//Hallar FPrima
        }//Hallar F
    }

    public void panelChanger(int panelNumber) {

        switch (panelNumber) {
            case (WELCOME): {
                cl.show(this.getContentPane(), "Welcome");
                break;
            }
            case (MAIN): {
                cl.show(this.getContentPane(), "Main");
                break;
            }
            case (GRAFICAS): {
                cl.show(this.getContentPane(), "Grafica");
                break;
            }
            default: {
                JOptionPane.showMessageDialog(this, "Este Metodo debe tener uno de dos parametros validos");
            }
        }

    }

    public final void initComponents2() {
        panelWelcome = new PanelWelcome(this);
        panelMain = new PanelMain(this);
        panelGrafica = new PanelGraficas(this);

        add(panelWelcome, "Welcome");
        add(panelMain, "Main");
        add(panelGrafica, "Grafica");


        pack();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXHyperlink2 = new org.jdesktop.swingx.JXHyperlink();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        MenuSalir = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        MenuItemBlueSteel = new javax.swing.JMenuItem();
        MenuAutumn = new javax.swing.JMenuItem();
        MenuItemRavenGraphite = new javax.swing.JMenuItem();
        MenuItemOfficeBlue = new javax.swing.JMenuItem();
        MenuItemOfficeSilver = new javax.swing.JMenuItem();
        MenuAcercaDe = new javax.swing.JMenuItem();

        jXLabel1.setText("jXLabel1");

        jXHyperlink2.setText("jXHyperlink2");
        jXHyperlink2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink2ActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setFocusable(false);
        setResizable(false);
        getContentPane().setLayout(new java.awt.CardLayout());

        jMenu1.setText("Archivo");

        MenuSalir.setText("Salir");
        MenuSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuSalirActionPerformed(evt);
            }
        });
        jMenu1.add(MenuSalir);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Herramientas");

        jMenu3.setText("LookAndFeel");

        MenuItemBlueSteel.setText("BlueSteel");
        MenuItemBlueSteel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemBlueSteelActionPerformed(evt);
            }
        });
        jMenu3.add(MenuItemBlueSteel);

        MenuAutumn.setText("Autumn");
        MenuAutumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuAutumnActionPerformed(evt);
            }
        });
        jMenu3.add(MenuAutumn);

        MenuItemRavenGraphite.setText("RavenGraphite");
        MenuItemRavenGraphite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemRavenGraphiteActionPerformed(evt);
            }
        });
        jMenu3.add(MenuItemRavenGraphite);

        MenuItemOfficeBlue.setText("OfficeBlue");
        MenuItemOfficeBlue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemOfficeBlueActionPerformed(evt);
            }
        });
        jMenu3.add(MenuItemOfficeBlue);

        MenuItemOfficeSilver.setText("OfficeSilver");
        MenuItemOfficeSilver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuItemOfficeSilverActionPerformed(evt);
            }
        });
        jMenu3.add(MenuItemOfficeSilver);

        jMenu2.add(jMenu3);

        MenuAcercaDe.setText("Acerca de");
        MenuAcercaDe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuAcercaDeActionPerformed(evt);
            }
        });
        jMenu2.add(MenuAcercaDe);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXHyperlink2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink2ActionPerformed
        JOptionPane.showMessageDialog(this, "Funciona");//Un dialogo pendejo
    }//GEN-LAST:event_jXHyperlink2ActionPerformed

    private void MenuItemBlueSteelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuItemBlueSteelActionPerformed
        try {
            if (blueSteelLAF == null) {
                blueSteelLAF = new SubstanceBusinessBlueSteelLookAndFeel();
            }
            UIManager.setLookAndFeel(blueSteelLAF);
            configureAllHyperlinksColor(Color.blue);
            SwingXUtilities.updateAllComponentTreeUIs();

        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuItemBlueSteelActionPerformed

    private void MenuItemRavenGraphiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuItemRavenGraphiteActionPerformed
        try {
            if (graphiteLAF == null) {
                graphiteLAF = new SubstanceGraphiteGlassLookAndFeel();
            }
            UIManager.setLookAndFeel(graphiteLAF);
            configureAllHyperlinksColor(Color.white);

            SwingXUtilities.updateAllComponentTreeUIs();
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuItemRavenGraphiteActionPerformed

    private void MenuItemOfficeBlueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuItemOfficeBlueActionPerformed
        try {
            if (officeBlueLAF == null) {
                officeBlueLAF = new SubstanceOfficeBlue2007LookAndFeel();
            }
            UIManager.setLookAndFeel(officeBlueLAF);
            configureAllHyperlinksColor(Color.darkGray);

            SwingXUtilities.updateAllComponentTreeUIs();
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuItemOfficeBlueActionPerformed

    private void MenuItemOfficeSilverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuItemOfficeSilverActionPerformed
        try {
            if (officeSilverLAF == null) {
                officeSilverLAF = new SubstanceOfficeSilver2007LookAndFeel();
            }
            UIManager.setLookAndFeel(officeSilverLAF);
            configureAllHyperlinksColor(Color.black);
            SwingXUtilities.updateAllComponentTreeUIs();
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuItemOfficeSilverActionPerformed

    private void MenuSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuSalirActionPerformed
        int i = JOptionPane.showConfirmDialog(this, "Estas Seguro que deseas Salir", "Confirmar", JOptionPane.YES_NO_OPTION);
        switch (i) {
            case (JOptionPane.YES_OPTION): {
                System.exit(0);
                break;
            }
            default: {
                break;
            }
        }
    }//GEN-LAST:event_MenuSalirActionPerformed

    private void MenuAcercaDeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuAcercaDeActionPerformed
        if (about == null) {
            about = new DialogoAboutUs(this, true);
        }

        about.setVisible(true);
    }//GEN-LAST:event_MenuAcercaDeActionPerformed

    private void MenuAutumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuAutumnActionPerformed
        try {
            if (autumnLAF == null) {
                autumnLAF = new SubstanceAutumnLookAndFeel();
            }
            UIManager.setLookAndFeel(autumnLAF);
            configureAllHyperlinksColor(Color.black);
            SwingXUtilities.updateAllComponentTreeUIs();

        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NuMethodApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MenuAutumnActionPerformed

    public static void helpLauncher(String pagina) {
        try {
            String t = System.getProperties().getProperty("user.dir");
            File fa = new File(t);
            String h = "cmd.exe /c "
                    + " " + "\"" + fa.getAbsolutePath() + File.separator + "NuMethodHelp" + File.separator + pagina + "\"";
            Runtime.getRuntime().exec(h);
        } catch (IOException ex) {
            System.out.println("Oops, Not Workin");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame.setDefaultLookAndFeelDecorated(true);
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NuMethodApp().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MenuAcercaDe;
    private javax.swing.JMenuItem MenuAutumn;
    private javax.swing.JMenuItem MenuItemBlueSteel;
    private javax.swing.JMenuItem MenuItemOfficeBlue;
    private javax.swing.JMenuItem MenuItemOfficeSilver;
    private javax.swing.JMenuItem MenuItemRavenGraphite;
    private javax.swing.JMenuItem MenuSalir;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink2;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    // End of variables declaration//GEN-END:variables
}
